<template>
    <duv>
        <avue-form :option="formOption" v-model="form" :upload-after="uploadAfter"></avue-form>
        <avue-crud ref="crud" :option="option" :data="data" @row-update="addUpdate" @row-save="rowSave">
        </avue-crud>
    </duv>
</template>
<script>
import { checkAdd } from "@/api/infoManagement/patientFile";
export default {
    props: {
        reportTypeId: {
            type: String,
            default: ''
        },
        patientId: {
            type: String,
            default: ''
        }
    },
    created() {
        console.log('this.reportTypeId', this.reportTypeId, this.patientId);
    },
    data() {
        const self = this
        return {
            form: {
                imgUrl: [],
                checkDate: '',
            },
            formOption: {
                submitBtn: false,
                emptyBtn: false,
                column: [
                    {
                        label: '报告图片',
                        prop: 'imgUrl',
                        type: 'upload',
                        drag: true,
                        hide: true,
                        listType: 'picture-img',
                        loadText: '图片上传中，请稍等',
                        span: 24,
                        tip: '只能上传jpg/png格式的图片',
                        headers: {
                            'Content-Type': 'multipart/form-data'
                        },
                        data: {
                            caseReportCode: self.reportTypeId
                        },
                        propsHttp: {
                            // fileName: 'multipartFile',
                            res: 'data',
                            url: 'link',
                        },
                        action: "/api/blade-platform/caseReport/put-file?code=minio"
                        // action: "/api/blade-platform/checklist/checkReportAnalysis"
                    }, {
                        label: "报告单时间",
                        prop: "checkDate",
                        type: 'date',
                        valueFormat: 'yyyy-MM-dd',
                    },
                ]
            },
            data: [],
            option: {
                addBtn: false,
                addRowBtn: false,
                cellBtn: true,
                menuWidth: 250,
                columnBtn: false,
                maxHeight: 500,
                column: [],
                column1: [{
                    label: '特征名称',
                    prop: 'name',
                    cell: true,
                    rules: [
                        {
                            required: true,
                            message: '请输入',
                            trigger: 'blur'
                        }
                    ]
                },
                {
                    label: '特征编号',
                    prop: 'name2',
                    cell: true,
                    rules: [
                        {
                            required: true,
                            message: '请输入',
                            trigger: 'blur'
                        }
                    ]
                },
                {
                    label: '药前',
                    prop: 'name3',
                    cell: true,
                    rules: [
                        {
                            required: true,
                            message: '请输入',
                            trigger: 'blur'
                        }
                    ]
                },
                {
                    label: '药后',
                    prop: 'name4',
                    cell: true,
                    rules: [
                        {
                            required: true,
                            message: '请输入',
                            trigger: 'blur'
                        }
                    ]
                },
                {
                    label: '改善率',
                    prop: 'name5',
                    cell: true,
                    rules: [
                        {
                            required: true,
                            message: '请输入',
                            trigger: 'blur'
                        }
                    ]
                },
                {
                    label: '检查时间',
                    prop: 'name6',
                    cell: true,
                    rules: [
                        {
                            required: true,
                            message: '请输入',
                            trigger: 'blur'
                        }
                    ]
                },
                ]
            }
        }
    },
    methods: {
        save() {
            const item = {
                checkDate: this.form.checkDate,
                ocrResult: JSON.stringify({
                    list: this.data
                }),
                patientId: this.patientId,
                picture: this.form.imgUrl,
                typeCode: this.reportTypeId,
            }
            checkAdd(item).then(() => {
                this.$emit('done')
            })
        },
        buildColumns(columns) {
            const arr = columns.map((column) => {
                return {
                    ...column,
                    cell: true,
                }
            })
            this.option.column = arr
        },
        uploadAfter(res, done) {
            console.log('上传后', res);
            const colums = res.colums
            this.form.checkDate = res.pictureData.time
            const list = res.pictureData.list
            this.buildColumns(colums)
            this.data = list
            done()
        },
        rowSave(form, done) {
            done()
            // this.$message.success(
            //     '新增数据' + JSON.stringify(form)
            // )
            // checkUpdate(item).then(() => {
            //     done()
            // })
        },
        addUpdate(form, index, done, loading) {
            // this.$message.success('模拟网络请求')
            setTimeout(() => {
                // this.$message.success('关闭按钮等待')
                loading()
            }, 1000)
            setTimeout(() => {
                // this.$message.success(
                //     '编辑数据' + JSON.stringify(form) + '数据序号' + index
                // )
                done()
            }, 2000)
        }
    }
}
</script>